
#include "NtCoreLib.h"
#include "NtCamera.h"

using namespace NT;
using namespace RENDERER;

NtCamera::NtCamera()
{
	Crt::MemSet(m_pos, sizeof(m_pos));
	Crt::MemSet(m_rot, sizeof(m_rot));
}


NtCamera::~NtCamera()
{

}

void NtCamera::SetPosition(float x, float y, float z)
{
	m_pos[0] = x;
	m_pos[1] = y;
	m_pos[2] = z;
}


void NtCamera::SetRotation(float x, float y, float z)
{
	m_rot[0] = x;
	m_rot[1] = y;
	m_rot[2] = z;
}

const D3DXVECTOR3 NtCamera::GetPosition()
{
	return D3DXVECTOR3(m_pos[0], m_pos[1], m_pos[2]);
}


const D3DXVECTOR3 NtCamera::GetRotation()
{
	return D3DXVECTOR3(m_rot[0], m_rot[1], m_rot[2]);
}


void NtCamera::Render()
{
	// setup the vector that points upwards;
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	
	// setup the position of the camera in the world.
	D3DXVECTOR3 pos(m_pos[0], m_pos[1], m_pos[2]);

	// setup where the camera is looking by default
	D3DXVECTOR3 look(0.0f, 0.0f, 1.0f);


	// set the yaw(Y axis), pitch(X), and roll(Z) rotations in radians.
	NtFloat pitch = m_rot[0] * NtMath<float>::DEG_TO_RAD;
	NtFloat yaw = m_rot[1] * NtMath<float>::DEG_TO_RAD;
	NtFloat roll = m_rot[2] * NtMath<float>::DEG_TO_RAD;

	// create the rotation matrix from the yaw, pitch and roll values
	D3DXMATRIX rotationMatrix;
	D3DXMatrixRotationYawPitchRoll(&rotationMatrix, yaw, pitch, roll);

	// transform the lookat and up vector by the rotation matrix so the view
	// is correctly rotated at the origin.
	D3DXVec3TransformCoord(&look, &look, &rotationMatrix);
	D3DXVec3TransformCoord(&up, &up, &rotationMatrix);

	// translate the rotated camera position to the location of the viewer.
	look = pos + look;

	// finally create the view matrix from the three updated vectors.
	D3DXMatrixLookAtLH(&m_viewMatrix, &pos, &look, &up);
}


void NtCamera::GetViewMatrix(D3DXMATRIX& view)
{
	view = m_viewMatrix;
}
